package com.halfsword.creativecraft.block.custom.machine;

import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.BaseEntityBlock;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.RenderShape;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityTicker;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape;
import org.jetbrains.annotations.Nullable;

/**
 * 所有机器方块的基类，封装通用功能
 * 1. 默认模型渲染
 * 2. 方块实体交互逻辑
 * 3. 通用移除处理
 */
public abstract class BaseBlockEntityBlock extends BaseEntityBlock {

    // 默认碰撞箱（完整方块）
    protected static final VoxelShape DEFAULT_SHAPE = Block.box(0, 0, 0, 16, 16, 16);

    protected BaseBlockEntityBlock(Properties properties) {
        super(properties);
    }

    // ==== 通用方法实现 ==== //
    @Override
    public VoxelShape getShape(BlockState state, BlockGetter world, BlockPos pos, CollisionContext context) {
        return DEFAULT_SHAPE;
    }

    @Override
    public RenderShape getRenderShape(BlockState state) {
        return RenderShape.MODEL;
    }

    @Override
    public void onRemove(BlockState state, Level level, BlockPos pos, BlockState newState, boolean isMoving) {
        if (state.getBlock() != newState.getBlock()) {
            BlockEntity blockEntity = level.getBlockEntity(pos);
            if (blockEntity != null) {
                handleBlockEntityDrops(level, pos, blockEntity);
            }
        }
        super.onRemove(state, level, pos, newState, isMoving);
    }

    @Override
    public InteractionResult use(BlockState state, Level level, BlockPos pos, Player player,
                                 InteractionHand hand, BlockHitResult hit) {
        if (!level.isClientSide() && player instanceof ServerPlayer serverPlayer) {
            BlockEntity blockEntity = level.getBlockEntity(pos);
            if (blockEntity != null) {
                return handleBlockEntityInteraction(serverPlayer, pos, blockEntity);
            }
            throw new IllegalStateException("方块实体提供器缺失！");
        }
        return InteractionResult.sidedSuccess(level.isClientSide());
    }

    // ==== 抽象方法（子类必须实现） ==== //
    @Nullable
    @Override
    public abstract BlockEntity newBlockEntity(BlockPos pos, BlockState state);

    @Nullable
    @Override
    public abstract <T extends BlockEntity> BlockEntityTicker<T> getTicker(
            Level level, BlockState state, BlockEntityType<T> type);

    // ==== 可重写的保护方法 ==== //
    protected void handleBlockEntityDrops(Level level, BlockPos pos, BlockEntity blockEntity) {
        // 默认空实现，子类按需重写
    }

    protected InteractionResult handleBlockEntityInteraction(ServerPlayer player, BlockPos pos, BlockEntity blockEntity) {
        // 默认空实现，子类必须重写具体交互
        return InteractionResult.PASS;
    }
}